package com.guoxw.headerdemo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import com.guoxw.headerdemo.bean.Music;
import com.guoxw.headerdemo.view.MusicFragment;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private final static String TAG = MainActivity.class.getName().toString();

    private ArrayList<Music> musicArrayList = new ArrayList<>();

    private ArrayList<MusicFragment> musicFragments = new ArrayList<>();

    private MusicFragment musicFragment = new MusicFragment();

    private MusicFragment musicFragment1 = new MusicFragment();

    private MusicFragment musicFragment2 = new MusicFragment();

    private ViewPager vp_main;

    private TabLayout tb_main;

    private AppBarLayout appbar_main;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp_main = findViewById(R.id.vp_main);

        tb_main = findViewById(R.id.tb_main);

        appbar_main = findViewById(R.id.appbar_main);


        ArrayList<String> list_title = new ArrayList<>();
        list_title.add("新闻");
        list_title.add("体育");
        list_title.add("娱乐");

        //设置TabLayout的模式,这里主要是用来显示tab展示的情况的
        //TabLayout.MODE_FIXED          各tab平分整个工具栏,如果不设置，则默认就是这个值
        //TabLayout.MODE_SCROLLABLE     适用于多tab的，也就是有滚动条的，一行显示不下这些tab可以用这个
        //                              当然了，你要是想做点特别的，像知乎里就使用的这种效果
        tb_main.setTabMode(TabLayout.MODE_FIXED);

        //设置tablayout距离上下左右的距离
        //tab_title.setPadding(20,20,20,20);

        //为TabLayout添加tab名称
        tb_main.addTab(tb_main.newTab().setText(list_title.get(0)));
        tb_main.addTab(tb_main.newTab().setText(list_title.get(1)));
        tb_main.addTab(tb_main.newTab().setText(list_title.get(2)));



        musicFragments.add(musicFragment);
        musicFragments.add(musicFragment1);
        musicFragments.add(musicFragment2);

        vp_main.setAdapter(new FragmentStatePagerAdapter(this.getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return musicFragments.get(position);
            }

            @Override
            public int getCount() {
                return musicFragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return list_title.get(position);
            }
        });



        tb_main.setupWithViewPager(vp_main);

    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "dispatchTouchEvent::   MainActivity MotionEvent.ACTION_DOWN ");
                break;

            case MotionEvent.ACTION_MOVE:

                Log.d(TAG, "dispatchTouchEvent::   MainActivity  MotionEvent.ACTION_MOVE");
                break;

            case MotionEvent.ACTION_UP:

                Log.d(TAG, "dispatchTouchEvent::   MainActivity  MotionEvent.ACTION_UP");
                break;

        }


        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "onTouchEvent::   MainActivity MotionEvent.ACTION_DOWN ");
                break;

            case MotionEvent.ACTION_MOVE:

                Log.d(TAG, "onTouchEvent::   MainActivity  MotionEvent.ACTION_MOVE");
                break;

            case MotionEvent.ACTION_UP:

                Log.d(TAG, "onTouchEvent::   MainActivity  MotionEvent.ACTION_UP");
                break;

        }

        return super.onTouchEvent(event);
    }
}